app.register.controller('CarProfitCtrl', function($scope, $rootScope,
		CarProfitSer, AppUtil, $http, $q,$routeParams) {
	
	loadFlag($routeParams.id,$scope);
	
	var myDate = new Date(); 
    function addzero(v) {if (v < 10) return '0' + v;return v.toString();}
    var monthday = myDate.getFullYear().toString() + '-'+addzero(myDate.getMonth() + 1) +"-01";
	var today = myDate.getFullYear().toString() + '-'+addzero(myDate.getMonth() + 1) + '-' +addzero(myDate.getDate());
	$("#confirmed_date1").val(monthday);
	$("#confirmed_date2").val(today);
	
	// 获取本地存储中的页码页数 tableID+page or size
	var page = AppUtil.GetLocalValue("carProfitTable_Page");
	var size = AppUtil.GetLocalValue("carProfitTable_Size");
	$scope.pagination = {
			totalItems :0,
		currentPage : page == undefined ? 1 : page,
		itemsPerPage : size == undefined ? 10 : size
	};

	$scope.SearchData = function() {
		$scope.postData={};
		// 点击查询按钮时,恢复页码为1
		$scope.pagination.currentPage = 1
		// 表单查询参数
		
		LoadList();
	};
	// 加载数据方法
	var LoadList = function() {
		if ($scope.postData == undefined)
			$scope.postData = {};
		var params = AppUtil.Params(".form_params");
		params = $.extend({}, params,$scope.selected);
		params["brand_text"] = angular.element('select[name=brand_code]').find("option:selected").text();
		params["series_text"] = angular.element('select[name=series_code]').find("option:selected").text();
		params["model_text"] = angular.element('select[name=model_code]').find("option:selected").text();
		// 当有查询参数时,重置页数
		if (AppUtil.Length(params) > 0) {
			if($scope.entity_name != null){
				params['entity_name'] = $scope.entity_name;
			}
			$scope.postData.params = params;
		}
		// 筛选查询参数
		var search_opt = AppUtil.GetSearchOpt();
		if (search_opt.length > 0) {
			$scope.postData.search_opt = search_opt;
		}
		$scope.postData.page = $scope.pagination.currentPage;
		$scope.postData.size = $scope.pagination.itemsPerPage;
		// 加载数据
		CarProfitSer.list($scope.postData).success(function(response) {
			AppUtil.remove_loading();
			$scope.pagination.totalItems = response.total;
			$scope.carProfit = response.data.data;
			$scope.carProfitSum = response.data.sum;
			setTimeout(check_table_tr,500);
		});
	}

	// 配置分页监听
	$scope.$watch('pagination.refresh', LoadList);

	//加载品牌车系车型
	$http.get("common/brands").success(function(data) {
		$scope.brands = data;
		$scope.brandscopy = data;
	});
	
	$scope.LoadEntityBrands=function(entity){
		$scope.currentEntity= entity.entity_code;
		$rootScope.search_entity_name = entity.entity_name;
		$http.get("common/LoadEntityBrands?entity_code="+entity.entity_code).success(function(data) {
			$scope.ch_brand="";
			$rootScope.brands = data;
		});	
	};
	
	//清空门店
	$scope.clearGroupBrandEntity = function(){
		$scope.currentEntity= "";
		$rootScope.search_entity_name='';
		
		$scope.brands =$scope.brandscopy;
		$scope.ch_brand = '';
	}
	
	//门店监听
	$rootScope.$watch('search_entity_name', function(newValue,oldValue,scope){
		if(newValue==''){
			$scope.clearGroupBrandEntity();
		}
	});
	
	$scope.$watch('ch_brand', function(newValue,oldValue,scope){
		$scope.LoadSeries($scope.ch_brand, $scope.currentEntity);	
	});
	
	$scope.$watch('ch_serie', function(newValue,oldValue,scope){
		$scope.LoadModels($scope.ch_serie, $scope.currentEntity);	
	});
	
	AppUtil.LoadGroups();
	
	// 导出
	$scope.ExportData = function() {
		AppUtil.ExportData($scope.postData);
	};
	
	
});

app.register.service('CarProfitSer', function($http, AppUtil) {

	// 查询数据
	this.list = function(postData) {
		AppUtil.loading();
		postData.start = (postData.page - 1 < 0 ? 0 : postData.page - 1)
				* postData.size;
		return $http.post('carProfit/list', postData).error(function() {
			alertMsg("提示", "系统出错,请稍后重试.");
			AppUtil.remove_loading();
		});
	};
	
});
